//VUE NVUE公用
const jmURL = 'https://api.leduotv.com/wp-api/getvodurl.php'
export default {
	data() {
		return {
			//jiurltext:'',//临时储存未解密的播放地址
			videoURL:[],//解密后的视频真实地址！
			autoPayer:false,//是否开始播放视频？用于解密完成后
			/* ===============================================
			 * =  如果 StrArraylength = StrArrayindex
			 * =  则执行自动播放视频，同样的，如果第index个视频
			 * =  遇到解密视频，就让StrArraylength-1,避免观察到
			 * =  StrArraylength一直不等于StrArrayindex
			 * =  ***************************************
			 * =  @StrArraylength 执行“批量拆分字符串后赋值”
			 * =  @StrArrayindex “解密完成后赋值。”
			 * ===============================================
			 */
			StrArraylength:0,//拆分字符串后的总条数
			StrArrayindex:0,//解密完成第几个视频
		}
	},
	onLoad(){
		
	},
	methods:{
		// leduotv
		
		/* ==================================
		 * =  以下是用于“乐多资源”的相关事件  =
		 * ==================================
		 */
		
		/* ============================
		 * =   批量拆分字符串          =
		 * ============================
		 */
		async myStrArr() {
			//显示showloading
			this.isUniShowLoading(true)
			
			//读入视频标题，给下方使用
			var PlayerTitle = this.videoitem[0].vod_name
			
			//var myStr = "一条 母狗 看上 一条 公狗";
			
			//读入未解密的播放链接
			var myStr = this.videoitem[0].vod_play_url
			//var strArray = myStr.split(" ");
			//匹配从 # 开始剪断字符串
			var strArray = myStr.split("#")
			console.log("strArray.length")
			console.log(strArray.length)
			console.log("共有"+strArray.length+"需要解密")
			//赋值给“拆分字符串后的总条数”,用于数据观察
			this.StrArraylength = strArray.length
			
			for(var i = 0; i < strArray.length; i++){
				
				//console.log(strArray[i])
				
				//匹配字符串内包含$的符号,截取前部分(qian)和后部分(ho)
				var s = strArray[i]
				let qian = s.substring(0, s.indexOf('$'))//$之前
				let ho = s.substring(s.indexOf('$') + 1, s.length)//$之后
				
				//定义入口参数
				let options = {
					title:PlayerTitle,//视频标题
					zytitle:qian,//综艺标题[如202010809期]
					url:jmURL,//解密地址
					data:{
						vid:ho,//需要解密的值
					},
					i,//第几集
					isShowLoading:false,//是否启用加载中提示[此参数在这个文件中暂已废弃]
				}
				
				//将截取的后播放字符串等参数传递给解密接口
				console.log("正在解密第"+Number(i+1)+"个视频")
				await this.getArrPlayerUrl(options)
			}
		},
		
		
		/* ============================
		 * =  批量解密所有视频真实链接  =
		 * ============================
		 */
		getArrPlayerUrl(options) {
			console.log("options")
			//console.log(options) //正式发布时，要注释，不可暴露地址
			let that = this
			let AppName = this.xtsetting.appname
			
			let index = options.i//第几个视频
			let title = options.title//视频标题
			let zytitle = options.zytitle//综艺标题
			var m3u8 = new Promise((resolve,reject)=> {
				uni.request({
					url:options.url,
					data:options.data,
					success: (res) => {
						console.log("res")
						console.log(res)
						var arrurl = {
							title: title+'   第'+Number(index+1)+'集   '+AppName+'免费看全网最新视频',
							title1: '第'+Number(index+1)+'集',
							zytitle: zytitle,//综艺标题
							url:res.data.url,
							qualityName:'原画',
							multiURLs: [{
								url: res.data.url,
								qualityName: '原画',
							}, ]
						}
						that.videoURL = that.videoURL.concat(arrurl)
						console.log("第"+Number(index+1)+"个视频解密完成")
						resolve("第"+Number(index+1)+"个视频解密完成")
						//赋值给“解密完成第几个视频”,用于数据观察
						that.StrArrayindex = index
					},
					fail: (err) => {
						console.log("err")
						console.log(err)
						console.log("第"+Number(index+1)+"个视频解密失败")
						reject("第"+Number(index+1)+"个视频解密失败")
						uni.showToast({
							title:"第"+Number(index+1)+"个视频解密失败"
						})
						//将“拆分字符串后的总条数”-1,避免观察出现不等于
						let StrARRleng = that.StrArraylength
						that.StrArraylength = Number(StrARRleng-1)
					},
					complete() {
						//
						//执行观察视频是否全部解密完成并自动播放
						that.isVideoAuto()
					}
				})
			})
			return m3u8
		},
		/* ==============================================
		 * =  判断StrArrayindex是不是等于StrArraylength  =
		 * =  @如果等于                                 =
		 * =  则autoPayer = true,自动播放视频            =
		 * =  @否则                                     =
		 * =  什么都不做，因为还在循环解密中              =
		 * =  ==========================================
		 */
		isVideoAuto(){
			let videolength = this.StrArraylength//拆分字符串后的总条数
			//因为this.StrArrayindex是从0开始，所以需要加1
			let videoindex = Number(this.StrArrayindex+1)//解密完成第几个视频
			console.log(videolength)
			console.log(videoindex)
			if (Number(videoindex) == Number(videolength)) {
				console.log("数据观察到：全部视频解密完毕")
				//设置自动播放状态为true,用于数据观察,自动播放
				this.autoPayer = true
				//console.log("开始自动播放第一个视频")
				
				//关闭showloading
				this.isUniShowLoading(false)
				
				console.log(this.videoURL)
				let SetVideoURLSync = this.videoURL
				//将解密后的所有数据存入本地缓存中，nvueVideo.js直接读取本地
				//uni.setStorageSync('jishuitem',SetVideoURLSync)//同步方法
				
				/* ========================================
				 * =  执行写入当前视频数据和储存当前视频数据  =   
				 * =========================================
				 */
				this.SetVideoitem()
				
			} else {
				console.log("videolength = " + videolength)
				console.log("videoindex = " + videoindex)
				console.log("数据观察到：第"+Number(videoindex+1)+"个视频正在解密")
			}
		},
		/* ============================
		 * =   解密中显示showLoading   =
		 * ==    全部解密完成后关闭     =
		 * ============================
		 */
		isUniShowLoading(val){
			if(val == true) {
				uni.showLoading({
					title:"转码中"
				})
			}else if (val == false) {
				uni.hideLoading()
			}else {
				console.log("ShowLoading未传参")
			}
		},
		
		
		/* ==============================================
		 * =  写入当前视频数据                            =
		 * =  (更新videoitem下的播放链接为已解密的)        =
		 * =  然后再把更新完毕的视频数据给储存视频数据方法   =
		 * ===============================================
		 */
		async SetVideoitem() {
			//将视频链接设置为空数组
			this.videoitem[0].vod_play_url = []
			//获取新的播放链接
			let videoURLS = this.videoURL
			//写入新的播放链接(已经解密过的)
			this.videoitem[0].vod_play_url = this.videoitem[0].vod_play_url.concat(videoURLS)
			/* ========================
			 * =  执行储存当前视频数据  =   
			 * ========================
			 */
			await this.SetStorVideoInfo()
		},
		
		
		/* =====================
		 * =  储存当前视频数据  =   
		 * =====================
		 */
		SetStorVideoInfo(){
			console.log("获取更新后的视频数据")
			//获取更新后的视频数据
			console.log(this.videoitem)
			let resData = this.videoitem
			var res = new Promise((resolve,reject) =>{
				let videoID = this.videoitem[0].vod_id
				// 数据缓存 异步
				uni.setStorage({
				    key:'videoID'+videoID,
				    data:resData,
				    success() {
				        console.log('视频数据缓存本地成功了')
						resolve('成功了')
				    },
				    fail() {
				        console.log('视频数据缓存本地失败了')
						reject('失败了')
				    }
				});
			})
			return res
		},
		
		
		
		
		
	}
}